package com.tools.common.object.type.handler;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.tools.common.container.MapEnum;
import com.tools.common.container.OwnPairs;
import com.tools.common.container.r.OnlyReadHashMap;
import com.tools.common.container.r.OnlyReadLinkedMap;
import com.tools.common.container.r.OnlyReadTreeMap;
import com.tools.common.exception.ParseRuntimeException;
import com.tools.common.object.C;
import com.tools.common.object.Note;
import com.tools.common.object.ObjKit;
import com.tools.common.object.json.DeserializerHandler;
import com.tools.common.object.json.JsonKit;
import com.tools.common.object.string.VerifyKit;
import com.tools.common.object.type.CTKit;

import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListMap;

/**
 * Map 集合的类型处理器
 * */
@Note("Map 集合的类型处理器")
public final class MapTypeHandler {

    private MapTypeHandler() {}

    /* ************************************************************************************************************
     *
     *          转换的功能方法
     *
     * ************************************************************************************************************
     * */

    @Note("将 source 转换为 Map 集合")
    public static Map castToMap(Object source, C<?> targetType) {
        Class<?> mainType = targetType.getMainType();
        if(HashMap.class.equals(mainType) || Map.class.equals(mainType) || AbstractMap.class.equals(mainType)) {
            return hashMap(source, targetType);
        }
        if(LinkedHashMap.class.equals(mainType)) {
            return linkedHashMap(source, targetType);
        }
        if(ConcurrentHashMap.class.equals(mainType)) {
            return concurrentHashMap(source, targetType);
        }
        if(TreeMap.class.equals(mainType)) {
            return treeMap(source, targetType);
        }
        if(ConcurrentSkipListMap.class.equals(mainType)) {
            return concurrentSkipListMap(source, targetType);
        }
        if(OnlyReadHashMap.class.equals(mainType)) {
            return onlyReadHashMap(source, targetType);
        }
        if(OnlyReadLinkedMap.class.equals(mainType)) {
            return onlyReadLinkedMap(source, targetType);
        }
        if(OnlyReadTreeMap.class.equals(mainType)) {
            return onlyReadTreeMap(source, targetType);
        }
        return otherMap(source, targetType);
    }


    @Note("使用 JsonParser 流模式将 JSON 转为 Map 集合")
    public static Map castToMap(JsonParser parser, C<?> targetType) {
        Class<?> mainType = targetType.getMainType();
        try {
            if(HashMap.class.equals(mainType)) {
                HashMap map = hashMap(targetType);
                return private_putInMap(parser, map, targetType);
            }
            if(LinkedHashMap.class.equals(mainType) || Map.class.equals(mainType) || AbstractMap.class.equals(mainType)) {
                LinkedHashMap map = linkedHashMap(targetType);
                return private_putInMap(parser, map, targetType);
            }
            if(ConcurrentHashMap.class.equals(mainType)) {
                ConcurrentHashMap map = concurrentHashMap(targetType);
                return private_putInMap(parser, map, targetType);
            }
            if(TreeMap.class.equals(mainType)) {
                TreeMap map = treeMap(targetType);
                try {
                    return private_putInMap(parser, map, targetType);
                } catch (ClassCastException e) {
                    TreeMap res = new TreeMap(DeserializerHandler.getDefaultComparator());
                    res.putAll(map);
                    return private_putInMap(parser, res, targetType);
                }
            }
            if(ConcurrentSkipListMap.class.equals(mainType)) {
                ConcurrentSkipListMap map = MapTypeHandler.concurrentSkipListMap(targetType);
                try {
                    return private_putInMap(parser, map, targetType);
                } catch (ClassCastException e) {
                    ConcurrentSkipListMap res = new ConcurrentSkipListMap(DeserializerHandler.getDefaultComparator());
                    res.putAll(map);
                    return private_putInMap(parser, res, targetType);
                }
            }
            if(OnlyReadHashMap.class.equals(mainType)) {
                HashMap tempMap = MapTypeHandler.onlyReadHashTempMap(targetType);
                Map map = private_putInMap(parser, tempMap, targetType);
                return new OnlyReadHashMap<>(map);
            }
            if(OnlyReadLinkedMap.class.equals(mainType)) {
                LinkedHashMap tempMap = MapTypeHandler.onlyReadLinkedTempMap(targetType);
                Map map = private_putInMap(parser, tempMap, targetType);
                return new OnlyReadLinkedMap<>(map);
            }
            if(OnlyReadTreeMap.class.equals(mainType)) {
                HashMap tempMap = MapTypeHandler.onlyReadHashTempMap(targetType);
                Map map = private_putInMap(parser, tempMap, targetType);
                Object second = targetType.getArgValue(1);
                if(second instanceof Comparator) {
                    return new OnlyReadTreeMap(map, (Comparator) second);
                }
                return new OnlyReadTreeMap(map, DeserializerHandler.getDefaultComparator());
            }
            Map map = (Map) ObjKit.buildObject(targetType);
            return private_putInMap(parser, map, targetType);
        } catch (Exception e) {
            throw new ParseRuntimeException(e);
        }
    }


    /* ************************************************************************************************************
     *
     *          构建空集合的方法
     *
     * ************************************************************************************************************
     * */


    @Note("新建空的 HashMap")
    public static HashMap hashMap(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        Object second = targetType.getArgValue(1);
        if(first instanceof Integer) {
            int f = (Integer) first;
            if(second instanceof Float || second instanceof Double) {
                float s = ((Number) second).floatValue();
                return new HashMap(f, s);
            }
            return new HashMap(f);
        }
        if(first instanceof Map) {
            return new HashMap((Map) first);
        }
        return new HashMap();
    }


    @Note("新建空的 LinkedHashMap")
    public static LinkedHashMap linkedHashMap(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        Object second = targetType.getArgValue(1);
        Object third = targetType.getArgValue(2);
        if(first instanceof Integer) {
            int f = (Integer) first;
            if(second instanceof Float || second instanceof Double) {
                float s = ((Number) second).floatValue();
                if(third instanceof Boolean) {
                    boolean t = (Boolean) third;
                    return new LinkedHashMap(f, s, t);
                }
                return new LinkedHashMap(f, s);
            }
            return new LinkedHashMap(f);
        }
        if(first instanceof Map) {
            return new LinkedHashMap<>((Map) first);
        }
        return new LinkedHashMap();
    }



    @Note("新建空的 ConcurrentHashMap")
    public static ConcurrentHashMap concurrentHashMap(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        Object second = targetType.getArgValue(1);
        Object third = targetType.getArgValue(2);
        if(first instanceof Integer) {
            int f = (Integer) first;
            if(second instanceof Float || second instanceof Double) {
                float s = ((Number) second).floatValue();
                if(third instanceof Integer) {
                    int t = (Integer) third;
                    return new ConcurrentHashMap(f, s, t);
                }
                return new ConcurrentHashMap(f, s);
            }
            return new ConcurrentHashMap(f);
        }
        if(first instanceof Map) {
            return new ConcurrentHashMap((Map) first);
        }
        return new ConcurrentHashMap();
    }


    @Note("新建空的 TreeMap")
    public static TreeMap treeMap(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Comparator) {
            return new TreeMap((Comparator) first);
        }
        if(first instanceof SortedMap) {
            SortedMap f = (SortedMap) first;
            try {
                return new TreeMap(f);
            } catch (ClassCastException e) {
                TreeMap treeMap = new TreeMap(DeserializerHandler.getDefaultComparator());
                treeMap.putAll(f);
                return treeMap;
            }
        }
        if(first instanceof Map) {
            Map f = (Map) first;
            try {
                return new TreeMap(f);
            } catch (ClassCastException e) {
                TreeMap treeMap = new TreeMap(DeserializerHandler.getDefaultComparator());
                treeMap.putAll(f);
                return treeMap;
            }
        }
        return new TreeMap();
    }


    @Note("新建空的 ConcurrentSkipListMap")
    public static ConcurrentSkipListMap concurrentSkipListMap(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Comparator) {
            return new ConcurrentSkipListMap((Comparator) first);
        }
        if(first instanceof SortedMap) {
            SortedMap f = (SortedMap) first;
            try {
                return new ConcurrentSkipListMap(f);
            } catch (ClassCastException e) {
                ConcurrentSkipListMap map = new ConcurrentSkipListMap(DeserializerHandler.getDefaultComparator());
                map.putAll(f);
                return map;
            }
        }
        if(first instanceof Map) {
            Map f = (Map) first;
            try {
                return new ConcurrentSkipListMap(f);
            } catch (ClassCastException e) {
                ConcurrentSkipListMap map = new ConcurrentSkipListMap(DeserializerHandler.getDefaultComparator());
                map.putAll(f);
                return map;
            }
        }
        return new ConcurrentSkipListMap();
    }


    @Note("获取空的临时的 HashMap，用于后续生成只读 HashMap -> OnlyReadHashMap")
    public static HashMap onlyReadHashTempMap(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Map) {
            return new HashMap((Map) first);
        }
        if(first instanceof OwnPairs) {
            OwnPairs p = (OwnPairs) first;
            return (HashMap) p.toMap(MapEnum.HASH);
        }
        return new HashMap();
    }


    @Note("获取空的临时的 LinkedHashMap，用于后续生成只读 LinkedHashMap -> OnlyReadLinkedMap")
    public static LinkedHashMap onlyReadLinkedTempMap(C<?> targetType) {
        Object first = targetType.getArgValue(0);
        if(first instanceof Map) {
            return new LinkedHashMap((Map) first);
        }
        if(first instanceof OwnPairs) {
            OwnPairs p = (OwnPairs) first;
            return (LinkedHashMap) p.toMap(MapEnum.LINKED);
        }
        return new LinkedHashMap();
    }



    /* ************************************************************************************************************
     *
     *          构建完整数据集合的功能方法
     *
     * ************************************************************************************************************
     * */

    @Note("HashMap 集合的类型转换")
    public static HashMap hashMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            HashMap map = hashMap(targetType);
            CTKit.mapToMapFillIn((Map) source, map, targetType);
            return map;
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            HashMap map = hashMap(targetType);
            CTKit.pairsToMapFillIn((OwnPairs) source, map, targetType);
            return map;
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (HashMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 java.util.HashMap");
        }
        Map<String, Object> sourceMap = ObjKit.allFieldValue(source);
        HashMap map = hashMap(targetType);
        CTKit.mapToMapFillIn(sourceMap, map, targetType);
        return map;
    }


    @Note("LinkedHashMap 集合的类型转换")
    public static LinkedHashMap linkedHashMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            LinkedHashMap map = linkedHashMap(targetType);
            CTKit.mapToMapFillIn((Map) source, map, targetType);
            return map;
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            LinkedHashMap map = linkedHashMap(targetType);
            CTKit.pairsToMapFillIn((OwnPairs) source, map, targetType);
            return map;
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (LinkedHashMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 java.util.LinkedHashMap");
        }
        Map<String, Object> sourceMap = ObjKit.allFieldValue(source);
        LinkedHashMap map = linkedHashMap(targetType);
        CTKit.mapToMapFillIn(sourceMap, map, targetType);
        return map;
    }


    @Note("ConcurrentHashMap 集合的类型转换")
    public static ConcurrentHashMap concurrentHashMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            ConcurrentHashMap map = concurrentHashMap(targetType);
            CTKit.mapToMapFillIn((Map) source, map, targetType);
            return map;
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            ConcurrentHashMap map = concurrentHashMap(targetType);
            CTKit.pairsToMapFillIn((OwnPairs) source, map, targetType);
            return map;
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (ConcurrentHashMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 java.util.concurrent.ConcurrentHashMap");
        }
        Map<String, Object> sourceMap = ObjKit.allFieldValue(source);
        ConcurrentHashMap map = concurrentHashMap(targetType);
        CTKit.mapToMapFillIn(sourceMap, map, targetType);
        return map;
    }

    @Note("TreeMap 集合的类型转换")
    public static TreeMap treeMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            TreeMap map = treeMap(targetType);
            try {
                CTKit.mapToMapFillIn((Map) source, map, targetType);
                return map;
            } catch (ClassCastException e) {
                TreeMap res = new TreeMap(DeserializerHandler.getDefaultComparator());
                res.putAll(map);
                CTKit.mapToMapFillIn((Map) source, res, targetType);
                return res;
            }
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            TreeMap map = treeMap(targetType);
            try {
                CTKit.pairsToMapFillIn((OwnPairs) source, map, targetType);
                return map;
            } catch (ClassCastException e) {
                TreeMap res = new TreeMap(DeserializerHandler.getDefaultComparator());
                res.putAll(map);
                CTKit.pairsToMapFillIn((OwnPairs) source, res, targetType);
                return res;
            }
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (TreeMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 java.util.TreeMap");
        }
        Map<String, Object> sourceMap = ObjKit.allFieldValue(source);
        TreeMap map = treeMap(targetType);
        try {
            CTKit.mapToMapFillIn(sourceMap, map, targetType);
            return map;
        } catch (ClassCastException e) {
            TreeMap res = new TreeMap(DeserializerHandler.getDefaultComparator());
            res.putAll(map);
            CTKit.mapToMapFillIn(sourceMap, res, targetType);
            return res;
        }
    }


    @Note("ConcurrentSkipListMap 集合的类型转换")
    public static ConcurrentSkipListMap concurrentSkipListMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            ConcurrentSkipListMap map = concurrentSkipListMap(targetType);
            try {
                CTKit.mapToMapFillIn((Map) source, map, targetType);
                return map;
            } catch (ClassCastException e) {
                ConcurrentSkipListMap res = new ConcurrentSkipListMap(DeserializerHandler.getDefaultComparator());
                res.putAll(map);
                CTKit.mapToMapFillIn((Map) source, res, targetType);
                return res;
            }
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            ConcurrentSkipListMap map = concurrentSkipListMap(targetType);
            try {
                CTKit.pairsToMapFillIn((OwnPairs) source, map, targetType);
                return map;
            } catch (ClassCastException e) {
                ConcurrentSkipListMap res = new ConcurrentSkipListMap(DeserializerHandler.getDefaultComparator());
                res.putAll(map);
                CTKit.pairsToMapFillIn((OwnPairs) source, res, targetType);
                return res;
            }
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (ConcurrentSkipListMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 java.util.concurrent.ConcurrentSkipListMap");
        }
        Map<String, Object> sourceMap = ObjKit.allFieldValue(source);
        ConcurrentSkipListMap map = concurrentSkipListMap(targetType);
        try {
            CTKit.mapToMapFillIn(sourceMap, map, targetType);
            return map;
        } catch (ClassCastException e) {
            ConcurrentSkipListMap res = new ConcurrentSkipListMap(DeserializerHandler.getDefaultComparator());
            res.putAll(map);
            CTKit.mapToMapFillIn(sourceMap, res, targetType);
            return res;
        }
    }


    @Note("只读 HashMap 集合的类型转换")
    public static OnlyReadHashMap onlyReadHashMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            HashMap temp = onlyReadHashTempMap(targetType);
            CTKit.mapToMapFillIn((Map) source, temp, targetType);
            return new OnlyReadHashMap(temp);
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            HashMap temp = onlyReadHashTempMap(targetType);
            CTKit.pairsToMapFillIn((OwnPairs) source, temp, targetType);
            return new OnlyReadHashMap(temp);
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (OnlyReadHashMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 com.tools.common.container.r.OnlyReadHashMap");
        }
        Map<String, Object> sourceMap = ObjKit.allFieldValue(source);
        HashMap map = onlyReadHashTempMap(targetType);
        CTKit.mapToMapFillIn(sourceMap, map, targetType);
        return new OnlyReadHashMap(map);
    }


    @Note("只读 LinkedHashMap 集合的类型转换")
    public static OnlyReadLinkedMap onlyReadLinkedMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            LinkedHashMap temp = onlyReadLinkedTempMap(targetType);
            CTKit.mapToMapFillIn((Map) source, temp, targetType);
            return new OnlyReadLinkedMap(temp);
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            LinkedHashMap temp = onlyReadLinkedTempMap(targetType);
            CTKit.pairsToMapFillIn((OwnPairs) source, temp, targetType);
            return new OnlyReadLinkedMap(temp);
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (OnlyReadLinkedMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 com.tools.common.container.r.OnlyReadLinkedMap");
        }
        Map<String, Object> sourceMap = ObjKit.allFieldValue(source);
        LinkedHashMap map = onlyReadLinkedTempMap(targetType);
        CTKit.mapToMapFillIn(sourceMap, map, targetType);
        return new OnlyReadLinkedMap(map);
    }


    @Note("生成 OnlyReadTreeMap 只读集合")
    public static OnlyReadTreeMap onlyReadTreeMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            HashMap tempMap = onlyReadHashTempMap(targetType);
            CTKit.mapToMapFillIn((Map) source, tempMap, targetType);
            Object second = targetType.getArgValue(1);
            if(second instanceof Comparator) {
                return new OnlyReadTreeMap(tempMap, (Comparator) second);
            }
            return new OnlyReadTreeMap(tempMap, DeserializerHandler.getDefaultComparator());
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            HashMap tempMap = onlyReadHashTempMap(targetType);
            CTKit.pairsToMapFillIn((OwnPairs) source, tempMap, targetType);
            Object second = targetType.getArgValue(1);
            if(second instanceof Comparator) {
                return new OnlyReadTreeMap(tempMap, (Comparator) second);
            }
            return new OnlyReadTreeMap(tempMap, DeserializerHandler.getDefaultComparator());
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (OnlyReadTreeMap) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 com.tools.common.container.r.OnlyReadTreeMap");
        }
        Map<String, Object> map = ObjKit.allFieldValue(source);
        HashMap tempMap = onlyReadHashTempMap(targetType);
        CTKit.mapToMapFillIn(map, tempMap, targetType);
        Object second = targetType.getArgValue(1);
        if(second instanceof Comparator) {
            return new OnlyReadTreeMap(tempMap, (Comparator) second);
        }
        return new OnlyReadTreeMap(tempMap, DeserializerHandler.getDefaultComparator());
    }


    @Note("生成其他未知类型的 Map 集合")
    public static Map otherMap(Object source, C<?> targetType) {
        Class<?> sourceClass = source.getClass();
        if(Map.class.isAssignableFrom(sourceClass)) {
            Map map = (Map) ObjKit.buildObject(targetType);
            CTKit.mapToMapFillIn((Map) source, map, targetType);
            return map;
        }
        if(OwnPairs.class.isAssignableFrom(sourceClass)) {
            Map map = (Map) ObjKit.buildObject(targetType);
            CTKit.pairsToMapFillIn((OwnPairs) source, map, targetType);
            return map;
        }
        if(String.class.equals(sourceClass)) {
            String json = (String) source;
            if(VerifyKit.isJsonObject(json)) {
                return (Map) ObjKit.jsonParserToRealValue(json, targetType);
            }
            throw new ClassCastException("无法将非对象类型的 JSON 字符串 【" + json + "】 转为 " + targetType.getMainType().getName());
        }
        Map<String, Object> map = ObjKit.allFieldValue(source);
        Map result = (Map) ObjKit.buildObject(targetType);
        CTKit.mapToMapFillIn(map, result, targetType);
        return result;
    }


    /* *******************************************************************************************
     *
     *          私有逻辑
     *
     * *******************************************************************************************
     * */

    @Note("私有逻辑：将 JsonParser 转为 Map 集合的具体逻辑")
    private static Map private_putInMap(JsonParser parser, Map map, C<?> targetType) throws IOException {
        String fieldName;
        if(targetType.hasGenericType()) {
            C<?> keyType = targetType.generic(0);
            C<?> valueType = targetType.generic(1);
            while (parser.nextToken() != JsonToken.END_OBJECT) {
                if (parser.currentToken() == JsonToken.FIELD_NAME) {
                    fieldName = parser.getCurrentName();
                    parser.nextToken();
                    Object key = ObjKit.stringToRealValue(fieldName, keyType);
                    Object value = JsonKit.parseToken(parser, valueType);
                    map.put(key, ObjKit.toRealValue(value, valueType));
                }
            }
            return map;
        }
        while (parser.nextToken() != JsonToken.END_OBJECT) {
            if (parser.currentToken() == JsonToken.FIELD_NAME) {
                fieldName = parser.getCurrentName();
                parser.nextToken();
                map.put(fieldName, JsonKit.parseToken(parser));
            }
        }
        return map;
    }

}
